//begin of show.js
var pages = pages || {};

(function(p) {
  p.show = {
    init: function() {
      _init();
      _setHighLight();
    }
  };
  //local function
  function _init() {
    $(".content_title_procon").on('click', function() {
      var me = $(this);
      $.ajax({
        url: 'api.asp',
        type: 'POST',
        data: {
          "action": "article.like",
          "id": me.attr('funcid')
        },
        dataType: 'text',
        timeout: 1000,
        async: true,
        error: function() {
          //获取失败，就继续获取
        },
        success: function(result) {
          var obj = JSON.parse(result);
          if (obj) {
            if (obj.state == 'error' && obj.message == 'nochange') {
              if (obj.message == 'nochange') {
                alert('请勿重复点赞！');
              } else {
                alert(obj.message);
                return;
              }
            } else {
              $("#likes_text").text(JSON.parse(obj.data).likes);
            }
          }
          me.removeClass('unlock');
          me.addClass('lock');
          me.unbind("click"); //移除click
        }
      });
    })
    _bind_procon();
    _bind_comment_submit();
  }

  function _setHighLight() {
    SyntaxHighlighter.vars.discoveredBrushes = null;
    SyntaxHighlighter.autoloader(
      'js jscript javascript  /js/codebeautify/shBrushJScript.js',
      'vb vbnet              /js/codebeautify/shBrushVb.js',
      'ruby                   /js/codebeautify/shBrushRuby.js',
      'c     /js/codebeautify/shBrushCpp.js'
    );
    SyntaxHighlighter.all();
    setTimeout(function() {
      $("div[id^='highlighter']").attr("style", "overflow-y:hidden !important;height:100%");
    }, 1000);
  }

  function _bind_comment_submit() {
    $('.add-comment > button').on('click', function() {
      $.ajax({
        url: "/api.asp",
        type: "post",
        data: {
          'action': 'article.newcomment',
          'function_id': $('#function_id').val(),
          'content': $('#comment_edit').val(),
          'nick_name': $('#nick_name').val()
        },
        success: function(data) {
          var oRet = pages.common.parseJSON(data);
          var content = $('<div />').text(oRet.content).html();
          content = content.replace(/\n/g, "<br />");

          var data_struct = {
            'tagName': 'div',
            'attributes': {
              'class': 'panel-body narrow-gap'
            },
            'children': [{
              'tagName': 'div',
              'attributes': {
                'class': 'comment-text'
              },
              'content': content
            }, {
              'tagName': 'div',
              'attributes': {
                'class': 'comment-operation'
              },
              'children': [{
                'tagName': 'ul',
                'attributes': {
                  'class': 'list-inline'
                },
                'children': [{
                  'tagName': 'li',
                  'attributes': {
                    'class': 'thumbs-up',
                    'data-id': oRet.id.toString()
                  },
                  'children': [{
                    'tagName': 'i',
                    'attributes': {
                      'class': 'fa fa-thumbs-up operation'
                    },
                    'children': [{
                      'tagName': 'span',
                      'attributes': {
                        'class': 'num'
                      },
                      'content': '0'
                    }]
                  }]
                }, {
                  'tagName': 'li',
                  'attributes': {
                    'class': 'thumbs-down',
                    'data-id': oRet.id.toString()
                  },
                  'children': [{
                    'tagName': 'i',
                    'attributes': {
                      'class': 'fa fa-thumbs-down operation'
                    },
                    'children': [{
                      'tagName': 'span',
                      'attributes': {
                        'class': 'num'
                      },
                      'content': '0'
                    }]
                  }]
                }, {
                  'tagName': 'li',
                  'attributes': {
                    'class': 'sub-text'
                  },
                  content: oRet.created_at
                }, {
                  'tagName': 'li',
                  'attributes': {
                    'class': 'sub-text'
                  },
                  content: oRet.nick_name
                }]
              }]
            }]
          };

          var oDiv = pages.common.json2html(data_struct);
          if ($('.narrow-gap').length > 0) {
            $(oDiv).insertAfter('.narrow-gap:last');
          } else {
            $(oDiv).insertAfter('.comment-label:last');
          }

          $('#comment_edit').val('');
          $('#comment_edit').focus();
        }
      });
    });
  }

  function _bind_procon() {
    $(document).delegate('.thumbs-up', 'click', function() {
      var that = this;
      $.ajax({
        url: "/api.asp",
        type: "post",
        data: {
          'action': 'article.comment.like',
          'id': this.getAttribute('data-id')
        },
        success: function(data) {
          if (data.length > 0) {
            $(that).find('span').html(data);
          }
        }
      });
    });

    $(document).delegate('.thumbs-down', 'click', function() {
      var that = this;
      $.ajax({
        url: "/api.asp",
        type: "post",
        data: {
          'action': 'article.comment.dislike',
          'id': this.getAttribute('data-id')
        },
        success: function(data) {
          if (data.length > 0) {
            $(that).find('span').html(data);
          }
        }
      });
    });
  }

})(pages);